package com.mrdeer.web.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mrdeer.common.entity.beans.PageEntity;
import com.mrdeer.common.entity.dto.DictDataDTO;
import com.mrdeer.common.entity.sys.DictData;
import com.mrdeer.common.exception.BusinessException;
import com.mrdeer.common.exception.NotFoundException;
import com.mrdeer.web.mapper.DictDataMapper;
import com.mrdeer.web.service.sys.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典数据接口实现
 */
@Slf4j
@Service
@Transactional
public class SysDictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements SysDictDataService {

    @Resource
    private DictDataMapper dictDataMapper;

    @Override
    public DictData add(DictData t) {
        DictData select = getById(t.getId());
        if (null != select) {
            throw new BusinessException("The id already exists in the dict_data list");
        }
        if (dictDataMapper.insert(t) > 0) {
            return dictDataMapper.selectById(t.getId());
        }
        throw new BusinessException("The id add to the dict_data list failed");
    }

    @Override
    public boolean delete(String id) {
        DictData dictData = getById(id);
        if (null == dictData) {
            throw new BusinessException("The dict_data does not exist in the dict_data list");
        }
        return dictDataMapper.deleteById(id) > 0;
    }

    @Override
    public DictData update(DictData t) {
        if (dictDataMapper.updateById(t) > 0) {
            DictData select = dictDataMapper.selectById(t.getId());
            return select;
        }
        throw new BusinessException("The dictData update failed in the dict_data list");
    }

    @Override
    public DictData getById(String id) {
        return dictDataMapper.selectById(id);
    }

    @Override
    public Page list(DictDataDTO dto) {
        if (null == dto.getPage()) {
            dto.setPage(new PageEntity());
        }
        return dictDataMapper.selectPage(dto.getPage().convert(), fuzzyQuery(dto));
    }

    @Override
    public List<DictData> getByDictId(String dictId) {
        DictDataDTO dictDataDTO = new DictDataDTO();
        dictDataDTO.setDictId(dictId);
        List<DictData> dictDataList = dictDataMapper.selectList(fuzzyQuery(dictDataDTO));
        if (null == dictDataList || dictDataList.size() < 1) {
            throw new NotFoundException("The dict_data does not exist");
        }
        return dictDataList;
    }

    @Override
    public LambdaQueryWrapper<DictData> fuzzyQuery(DictDataDTO dto) {
        LambdaQueryWrapper<DictData> queryWrapper = Wrappers.<DictData>query().lambda();
        if (null != queryWrapper) {
            if (StringUtils.isNotBlank(dto.getDictId())) {
                queryWrapper.like(DictData::getDictId, dto.getDictId());
            }
        }
        return queryWrapper;
    }
}
